//
//  ZLColorExtension.swift
//  ZLExtension
//
//  Created by KDWL on 2023/1/9.
//

import Foundation
/// 快捷方式
public func ZLColor(_ hex: String, alpha: CGFloat = 1) -> UIColor {
    return UIColor.init(hex, alpha: alpha)
}
/// 随机色
public func ZLRandomColor(alpha: CGFloat = 1) -> UIColor {
    return UIColor.zl_random(alpha: alpha)
}

public extension UIColor {
    /// 16进制转换颜色
    convenience init(_ hexString: String, alpha: CGFloat = 1) {
        var hex = hexString
        if hex.hasPrefix("#") {
            hex.remove(at: hexString.startIndex)
        }
        if hex.hasPrefix("0x") {
            let index = hexString.index(hexString.startIndex, offsetBy: 2)
            hex.remove(at: index)
        }
        if hex.count != 6 {
            self.init(white: 0, alpha: 1)
            return
        }
        var red: Int64 = 0, green: Int64 = 0, blue: Int64 = 0
        Scanner(string: hex.zl_subString(to: 2)).scanHexInt64(&red)
        Scanner(string: hex.zl_subString(local: 2, length: 2)).scanHexInt64(&green)
        Scanner(string: hex.zl_subString(from: 4)).scanHexInt64(&blue)
        self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: alpha)
    }
    /// 随机色
    class func zl_random(alpha: CGFloat = 1) -> UIColor {
        let red = CGFloat(arc4random() % 256) / 255.0
        let green = CGFloat(arc4random() % 256) / 255.0
        let blue = CGFloat(arc4random() % 256) / 255.0
        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
}

public extension UIColor {
    /// 线浅色
    static var zl_lineLight: UIColor {
        get {
            return ZLColor("#F8F9F9")
        }
    }
    /// 线深色
    static var zl_lineDark: UIColor {
        get {
            return ZLColor("#DEDEE0")
        }
    }
}
